<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace Admin\Controller;

use Admin\Controller\AdminController;
use Common\Api\ExchangeLogApi;
use Admin\Api\DatatreeApi;
use Admin\Api\MemberApi;
use Admin\Api\MessageApi;
use Admin\Model\MessageModel;

class ScoreExLogController extends AdminController{
	protected function _initialize() {
        parent::_initialize();
  }

  public function index(){
  	//查询代发货的记录
    $this->assign('status',0);
  	$r = apiCall(ExchangeLogApi::PAGE,array(array('exchange_status'=>0)));
  	if($r['status'])
  	{
  	 	$this->assignTitle('积分商品兑换记录');
	  	$this->assign('list',$r['info']['list']);
	  	$this->assign('show',$r['info']['show']);
	  	$this->display();
	  }else{
	  	$this->error('ExchangeLogApi : ERROR');
	  }
  }

  public function express($id){

    if(IS_GET){
      $r = apiCall(DatatreeApi::QUERY_NO_PAGING,array(array('parentid'=>6003)));
      if(!$r['status']) $this->error('Express_COMPANY : GET : ERROR');
      // dump($r['info']);exit;
      $this->assign('eCompanys',$r['info']);
      $r = apiCall(ExchangeLogApi::GET_INFO,array(array('id'=>$id)));
      if(!$r['status']) $this->error('ExchangeLogApi : GET_INFO : ERROR');
      $this->assignTitle($r['info']['pname'].' - 积分商品发货');
      // $this->assign('entry',$r['info']['pname']);
      $this->assign('entry',$r['info']);
      $this->display();
    }elseif(IS_POST){
      $eid = I('post.express_company');
      $r   = apiCall(DatatreeApi::GET_INFO,array(array('id'=>$eid)));
      if(!$r['status']) $this->error('Express_COMPANY : GET : ERROR');
      $ecs = $r['info'];
      $map = array();
      $no  = trim(I('post.express_no',''));
      if(preg_match("/[A-Za-z0-9]+/",$no))  $map['express_no'] = $no;
      else $this->error('快递编号格式错误');
      $map['express_company'] = $ecs['name'];
      $map['express_code']    = $ecs['code'];
      $map['exchange_status'] = 1;
      unset($ecs);unset($no);
      // dump($map);exit;
      $r = apiCall(ExchangeLogApi::SAVEBYID,array($id,$map));
      // dump($r);exit;
      // echo M()->getLastSql();exit;
      if(!$r['status'] || !$r['info']) $this->error('保存失败或未作更改');
      $r     = apiCall(ExchangeLogApi::GET_INFO,['id'=>$id]);
      if(!$r['status'] || !$r['info']) $this->error('无此记录');
      $uid   = $r['info']['uid'];
      $pname = $r['info']['pname'];
      //消息推送
      $entity = array(
          'from_id' =>0,
          'title'   =>MessageModel::MESSAGE_ORDER,
          'content' =>'您兑换的'.$pname.'已发货',
          'summary' =>'您兑换的'.$pname.'已发货',
          'extra'   =>'', //消息记录中的
      );
      $after_open = array('type'=>'go_activity','param'=>MessageModel::MESSAGE_ORDER_ACTIVITY,'extra'=>array()); //推送参数
      $r = apiCall(MessageApi::PUSH_MESSAGE_WITH_TYPE,array(MessageModel::MESSAGE_ORDER,$entity,$uid,false,$after_open));
      if(!$r['status']) $this->error($r['info']);
      $this->success('发货成功',U('ScoreExLog/index'));
    }
  }

  public function edit($id){
    if(IS_GET){
      $r = apiCall(DatatreeApi::QUERY_NO_PAGING,array(array('parentid'=>6003)));
      if(!$r['status']) $this->error('Express_COMPANY : GET : ERROR');
      $this->assign('eCompanys',$r['info']);
      $smap = array(
        array('id'=>-2,'name'=>'已回收'),
        array('id'=>-1,'name'=>'待付款'),
        array('id'=> 0,'name'=>'待发货'),
        array('id'=> 1,'name'=>'已发货')
      );
      $this->assign('smap',$smap);
    	$r = apiCall(ExchangeLogApi::GET_INFO,array(array('id'=>$id)));
      if(!$r['status']) $this->error('ExchangeLogApi : GET_INFO : ERROR');
      $this->assignTitle($r['info']['pname'].' - 兑换记录');
      $this->assign('entry',$r['info']);
      $this->display();
    }elseif(IS_POST){

      $eid    = I('post.express_company');
      $status = I('post.exchange_status','');
      $r = apiCall(DatatreeApi::GET_INFO,array(array('id'=>$eid)));
      if(!$r['status']) $this->error('Express_COMPANY : GET : ERROR');
      $ecs = $r['info'];

      $map = array();
      $no = trim(I('post.express_no',''));
      if(preg_match("/[A-Za-z0-9]+/",$no))  $map['express_no'] = $no;
      else $this->error('快递编号格式错误');
      $map['express_company'] = $ecs['name'];
      $map['express_code']    = $ecs['code'];
      if($status !== '')      $map['exchange_status'] = $status;
      $r = apiCall(ExchangeLogApi::SAVEBYID,array($id,$map));
      // dump($r);exit;
      // echo M()->getLastSql();exit;
      if(!$r['status'] || !$r['info']) $this->error('保存失败或未作更改');
      $this->success('保存成功');
    }
  }

  //删除 已发货的兑换记录 （只是将exchange_status 改为-2）
  public function delete($id){
    $r = apiCall(ExchangeLogApi::DELETE,array($id));
    // dump($r);exit;
    if($r['status']) $this->success('删除成功');
    else $this->error('删除失败');
  }

  public function search($p=1){
    $kword = I('kword','');
    $status = I('status',99,'intval');
    // dump($kword);exit;
    $this->assignTitle($kword.' - 积分商品兑换记录搜索结果');
    $where = array();
    if($kword){
      // $where['id']     = array('like','%'.intval($kword).'%');
      // $where['name']   = array('like','%'.$kword.'%');
      // $where['_logic'] = 'OR';
      if(intval($kword))
      $where['_string']= " (`id` like '%".$kword."%' or `pname` like '%".$kword."%') ";
      else
      $where['_string']= " `pname` like '%".$kword."%' ";
    }
    if($status !=99) $where['exchange_status'] = $status;

  	$this->assign('status',$status);
    $this->assign('kword',$kword);

    $r = apiCall(ExchangeLogApi::PAGE,array($where,array('curpage'=>$p,'size'=>10),'',array('kword'=>$kword,'status'=>$status)));
    // dump($r);exit;
    if($r['status'])
    {
      $this->assign('list',$r['info']['list']);
      $this->assign('show',$r['info']['show']);
	  	$this->display('index');
	  }else{
	  	$this->error('ExchangeLogApi : ERROR');
	  }
   }
}